package com.xmsx.back.controller.index;

import com.xmsx.back.results.Result;
import com.xmsx.back.service.index.RecruitmentService;
import com.xmsx.back.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/recruitment")
public class RecruitmentController {
    @Autowired
    RecruitmentService recruitmentService;

    /**
     * 获取tag列表
     *
     * @return
     */
    @GetMapping("/tagList")
    public Result<List<String>> getRecruitmentTagList() {
        List<String> tagList = recruitmentService.getTagList();
        if (!tagList.isEmpty()) {
            return Result.success(tagList);
        } else {
            return Result.error("不存在任何tag");
        }
    }

    /**
     * 按tag获取招聘信息列表
     *
     * @param pageCount
     * @param size
     * @return
     */
    @GetMapping("/detail")
    public Result<Map> getRecruitmentListByTag(@RequestParam("pageCount") int pageCount,
                                               @RequestParam("size") int size,
                                               @RequestParam("tag") String tag) {
        Pager pager = recruitmentService.findByTagByPager(pageCount, size, tag);
        Map myMap = new HashMap<String, String>();
        myMap.put("total", pager.getTotal());
        myMap.put("list", pager.getRows());
        return Result.success(myMap);
    }


    /**
     * 按关键字模糊查询
     *
     * @param pageCount
     * @param size
     * @param keyword
     * @return
     */
    @GetMapping("/search")
    public Result<Map> getRecruitmentListByKeyword(@RequestParam("pageCount") int pageCount,
                                                   @RequestParam("size") int size,
                                                   @RequestParam("keyword") String keyword) {
        Pager pager = recruitmentService.findByKeywordByPager(pageCount, size, keyword);
        Map myMap = new HashMap<String, String>();
        myMap.put("total", pager.getTotal());
        myMap.put("list", pager.getRows());
        return Result.success(myMap);
    }

    /**
     * 获取招聘信息列表
     *
     * @param pageCount
     * @param size
     * @return
     */
    @GetMapping("/home")
    public Result<Map> getRecruitmentList(@RequestParam("pageCount") int pageCount,
                                          @RequestParam("size") int size) {
        Pager pager = recruitmentService.findByTagByPager(pageCount, size);
        Map myMap = new HashMap<String, String>();
        myMap.put("total", pager.getTotal());
        myMap.put("list", pager.getRows());
        return Result.success(myMap);
    }
}
